package com.mycc.game.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mycc.game.common.Code;
import com.mycc.game.common.Result;
import com.mycc.game.damin.Npc;
import com.mycc.game.damin.Rules;
import com.mycc.game.damin.Scenario;
import com.mycc.game.service.RulesService;
import com.mycc.game.service.ScenarioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class RulesController {

    @Autowired
    private RulesService ruleService;

    //查询所有规则信息
    @GetMapping("/selRulesAll/{pageNumber}/{pageSize}")
    public Result selRulesAll(@PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Rules> page = ruleService.selAllRules(pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //根据id查询怪谈信息
    @GetMapping("/selRulesById/{id}/{pageNumber}/{pageSize}")
    public Result selRulesById(@PathVariable Integer id, @PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Rules> page = ruleService.selAllRulesById(id, pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //通过名字模糊查询怪谈信息
    @GetMapping("/selRulesByName/{content}/{pageNumber}/{pageSize}")
    public Result selRulesByName(@PathVariable String content, @PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
        IPage<Rules> page = ruleService.selAllRulesByContentByName(content, pageNumber, pageSize);
        if (page != null) {
            return new Result(Code.SELECT_OK, "ok", page);
        } else {
            return new Result(Code.SELECT_FAIL, "查询失败", null);
        }
    }

    //删除规则信息
    @GetMapping("/delRulesById/{id}")
    public Result delRulesById(@PathVariable Integer id) {
        boolean flag = ruleService.delRulesById(id);
        if (flag) {
            return new Result(Code.DELETE_OK, "删除成功", flag);
        } else {
            return new Result(Code.DELETE_FAIL, "删除失败", null);
        }
    }

    //新值规则信息
    @PostMapping("/addRules")
    public Result addRules(@RequestBody Rules rules) {
        boolean flag = ruleService.addRules(rules);
        if (flag) {
            return new Result(Code.ADD_OK, "添加成功", flag);
        } else {
            return new Result(Code.ADD_FAIL, "添加失败", null);
        }
    }

    //修改规则信息
    @PostMapping("/updRules/{id}")
    public Result updRules(@PathVariable Integer id, @RequestBody Rules rules) {
        boolean flag = ruleService.updRules(id, rules);
        if (flag) {
            return new Result(Code.ADD_OK, "修改成功", flag);
        } else {
            return new Result(Code.ADD_FAIL, "修改失败", null);
        }
    }
}

